﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.achievement {
    import flash.events.*;
    import reprise.controls.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import net.wooga.pets.constants.*;

    public class AchievementBookPopupMediator extends AbstractPopupMediator {

        public var achievementModel:AchievementModel;

        override public function onRegister():void{
            super.onRegister();
            this.addAchievements();
        }
        private function addAchievements():void{
            var _local2:AchievementVO;
            var _local1:Array = this.achievementModel.achievements;
            for each (_local2 in _local1) {
                this.addAchievement(_local2);
            };
        }
        private function addAchievement(_arg1:AchievementVO):void{
            var _local3:RewardVO;
            var _local4:RewardVO;
            var _local5:LabelButton;
            var _local6:RewardVO;
            var _local7:String;
            var _local8:String;
            var _local2:AchievementBox = this.view.createAchievementBox(_arg1.id);
            _local2.createHeadline(language.getTextByKey(("achievement_book_" + _arg1.id)));
            _local2.createFrameAndImage(_arg1.id);
            _local2.setIsLocked(_arg1.isLocked, language.getTextByKey("global_levelnumber", {levelnumber:_arg1.unlockLevel}));
            for each (_local3 in _arg1.rewards) {
                _local2.createMedal(_local3.medal, _local3.rewardCollected);
            };
            if (_arg1.hasNotCollectedReachedRewards()){
                _local4 = _arg1.getNotCollectedReachedRewards()[0];
                _local5 = _local2.createGetRewardButton(language.getTextByKey("achievement_getreward"));
                _local5.setData(_local4);
                _local5.addEventListener(MouseEvent.CLICK, this.getRewardButton_click);
            } else {
                if (_arg1.completed){
                    _local2.setCompleted(language.getTextByKey("achievement_book_complete"));
                } else {
                    _local6 = _arg1.getActiveReward();
                    if (_local6){
                        _local7 = ((_arg1.points + "/") + _local6.pointsNeeded);
                        _local2.createNumberLabel(_local7);
                        _local8 = language.getTextByKey("global_reward");
                        _local2.createRewardDisplay(_local8, _local6.rewardType, _local6.numberOfRewardsYouGet, _local6.xpYouGet);
                    };
                };
            };
        }
        private function getRewardButton_click(_arg1:MouseEvent):void{
            var _local2:LabelButton = LabelButton(_arg1.target);
            var _local3:RewardVO = RewardVO(_local2.data());
            _local3.rewardCollected = true;
            dispatch(new InfoEvent(EventConstants.COLLECT_ACHIEVEMENT_REWARD, _local3.id, _local3));
            dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.ACHIEVEMENT_SHARE_REWARD_POPUP, _local3));
            this.view.reset();
            this.addAchievements();
            dispatch(new Event(EventConstants.UPDATE_ACHIEVEMENTS));
            var _local4:Object = new Object();
            _local4.type = "achievements";
            _local4.st1 = "book";
            _local4.st2 = "get_reward";
            _local4.st3 = _local3.id;
            track(_local4);
        }
        private function get view():AchievementBookPopup{
            return (AchievementBookPopup(viewComponent));
        }
        override protected function close():void{
            super.close();
            var _local1:Object = new Object();
            _local1.type = "achievements";
            _local1.st1 = "book";
            _local1.st2 = "close";
            track(_local1);
        }

    }
}//package net.wooga.pets.views.popups.achievement 
